package ckw.gui.wizards;

import java.util.ArrayList;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.CheckStateChangedEvent;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.ICheckStateListener;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;

import ckw.database.DatabaseManager;
import ckw.database.VotersList;

public class WzdElectionPg3 extends WizardPage {
	private ArrayList<VotersList> votersListList;
	private Table table;
	private CheckboxTableViewer checkboxTableViewer;
	
	private class TableLabelProvider extends LabelProvider implements ITableLabelProvider {
		public Image getColumnImage(Object element, int columnIndex) {
			return null;
		}
		public String getColumnText(Object element, int columnIndex) {
			VotersList votersList = (VotersList)element;
			return votersList.getName() + " - " + votersList.getDescription();
		}
	}
	

	/**
	 * Create the wizard.
	 */
	public WzdElectionPg3() {
		super("wizardPage");
		setTitle("Dodaj wybory");
		setDescription("Krok 3. Wybierz listy glosujacych");
		
		votersListList = (ArrayList<VotersList>) DatabaseManager.getInstance().getAll(VotersList.class);
		for (int i = 0; i < votersListList.size(); i++) {
			VotersList votersList = votersListList.get(i);
			votersList = DatabaseManager.getInstance().get(VotersList.class, votersList.getId());
			votersListList.set(i, votersList);
		}
	}

	/**
	 * Create contents of the wizard.
	 * @param parent
	 */
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NONE);

		setControl(container);
		container.setLayout(new GridLayout(2, false));
		
		Composite composite = new Composite(container, SWT.NONE);
		GridData gd_composite = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
		gd_composite.widthHint = 452;
		gd_composite.heightHint = 273;
		composite.setLayoutData(gd_composite);
		GridLayout gl_composite = new GridLayout(1, false);
		gl_composite.verticalSpacing = 0;
		gl_composite.marginWidth = 0;
		gl_composite.marginHeight = 0;
		gl_composite.horizontalSpacing = 0;
		composite.setLayout(gl_composite);
		
		checkboxTableViewer = CheckboxTableViewer.newCheckList(composite, SWT.BORDER | SWT.FULL_SELECTION);
		table = checkboxTableViewer.getTable();
		GridData gd_table = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);
		gd_table.heightHint = 246;
		gd_table.widthHint = 425;
		table.setLayoutData(gd_table);
		checkboxTableViewer.setLabelProvider(new TableLabelProvider());
		checkboxTableViewer.addCheckStateListener(new ICheckStateListener() {
			@Override
			public void checkStateChanged(CheckStateChangedEvent arg0) {
				((VotersList) arg0.getElement()).toggle();
			}
		});
		for(VotersList votersList : votersListList) {
			checkboxTableViewer.add(votersList);
			if(votersList.getChecked()) {
				checkboxTableViewer.setChecked(votersList, true);
			}
		}
		
		Button btnWybierzCzlonkow = new Button(container, SWT.CENTER);
		btnWybierzCzlonkow.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false, 1, 1));
		btnWybierzCzlonkow.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				VotersList votersList = new VotersList();
				WzdElectionDlgVotersListAdd dlg = new WzdElectionDlgVotersListAdd(getShell(), votersList, false);
				if (dlg.open() == IDialogConstants.OK_ID) {
					checkboxTableViewer.add(votersList);
				}
			}
		});
		btnWybierzCzlonkow.setText("Utworz nowa liste");
	}

	public ArrayList<VotersList> getElectionVotersListList() {
		return votersListList;
	}
}
